/**
 *====================================================
 * 文件名称: FinanceProductServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年11月11日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.service;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.project.entity.FinanceProductEntity;
import com.yuexunit.finance.project.entity.lexicon.ProductScopeEnum;
import com.yuexunit.finance.project.entity.lexicon.ProductStateEnum;
import com.yuexunit.finance.project.entity.lexicon.ProductTypeEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;

/**
 * @ClassName: FinanceProductServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年11月11日 17:09:55
 */
@Implements(FinanceProductService.class)
public class FinanceProductServiceImpl implements FinanceProductService {

	@Override
	public Long createFinanceProduct(FinanceProductEntity entity) {
		if (entity.getDistrain() == 0 || entity.getDistrain() < 0) {
			entity.setDistrain(0);
		}
		entity.setState(ProductStateEnum.WAIT_SUBMIT);
		entity.setEnabled(Boolean.TRUE);
		entity.setTop(Boolean.FALSE);
		entity.setUpdateDateTime(DateUtil.now());
		entity.setCreateDateTime(DateUtil.now());
		PersistUtil.insert(entity);
		return entity.getProductId();
	}

	@Override
	public void disableFinanceProduct(Long productId) {
		FinanceProductEntity persist = inquireFinanceProduct(productId);
		persist.setEnabled(Boolean.FALSE);
		persist.setTop(Boolean.FALSE);
		persist.setUpdateDateTime(DateUtil.now());
		PersistUtil.update(persist);
	}

	@Override
	public void updateFinanceProduct(FinanceProductEntity entity) {
		FinanceProductEntity persist = inquireFinanceProduct(entity.getProductId());
		BeanUtils.copyProperties(entity, persist);
		persist.setUpdateDateTime(new Date());
		PersistUtil.update(persist);
	}

	@Override
	@Transactional
	public void renovateFinanceProduct(FinanceProductEntity persist) {
		PersistUtil.update(persist);
	}

	@Override
	public FinanceProductEntity inquireFinanceProduct(Long productId) {
		return PersistUtil.get(FinanceProductEntity.class, productId);
	}

	@Override
	public PageList<FinanceProductEntity> inquireFinanceProduct(Long enterpriseId, ProductScopeEnum productScope, ProductTypeEnum productType, ProductStateEnum state, String productName, Integer startDate, Integer endDate,
			PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceProductEntity.class);
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		criteria.add(Restrictions.eq("productScope", productScope));
		if (productType != null) {
			criteria.add(Restrictions.eq("productType", productType));
		}
		if (state != null) {
			criteria.add(Restrictions.eq("state", state));
		}
		if (startDate != null) {
			criteria.add(Restrictions.ge("endDate", startDate));
		}
		if (endDate != null) {
			criteria.add(Restrictions.le("startDate", endDate));
		}
		if (StringUtils.isNoneBlank(productName)) {
			criteria.add(Restrictions.like("productName", "%" + productName + "%"));
		}
		if (enterpriseId != null) {
			criteria.add(Restrictions.or(Restrictions.isNull("enterpriseId"), Restrictions.eq("enterpriseId", enterpriseId)));
		} else {
			criteria.add(Restrictions.isNull("enterpriseId"));
		}
		criteria.addOrder(Order.desc("top"));
		return PersistUtil.get(criteria, page);
	}
}
